<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房型名称" prop="roomName">
        <el-input v-model="queryParams.roomName" placeholder="请输入房型名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="英文名称" prop="englishName">
        <el-input
          v-model="queryParams.englishName"
          placeholder="请输入英文名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="房型分类" prop="roomClass">
        <el-select v-model="queryParams.roomClass" placeholder="请选择房型分类" clearable>
          <el-option v-for="dict in room_class" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- 注释 -->
      <!-- <el-form-item label="早餐类型" prop="breakfastType">
        <el-select v-model="queryParams.breakfastType" placeholder="请选择早餐类型" clearable>
          <el-option
            v-for="dict in breakfast_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房型面积" prop="floorArea">
        <el-input
          v-model="queryParams.floorArea"
          placeholder="请输入房型面积"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大入住人数" prop="maxOccupancy">
        <el-input
          v-model="queryParams.maxOccupancy"
          placeholder="请输入最大入住人数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始楼层" prop="startFloor">
        <el-input
          v-model="queryParams.startFloor"
          placeholder="请输入起始楼层"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束楼层" prop="endFloor">
        <el-input
          v-model="queryParams.endFloor"
          placeholder="请输入结束楼层"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in wdm_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家ID" prop="merchantId">
        <el-input
          v-model="queryParams.merchantId"
          placeholder="请输入商家ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序序号" prop="sortOrder">
        <el-input
          v-model="queryParams.sortOrder"
          placeholder="请输入排序序号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="标记信息" prop="tags">
        <el-select v-model="queryParams.tags" placeholder="请选择标记信息" clearable>
          <el-option v-for="dict in tags" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="基础价格/晚" prop="basePrice">
        <el-input
          v-model="queryParams.basePrice"
          placeholder="请输入基础价格/晚"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="促销价格/晚" prop="promoPrice">
        <el-input
          v-model="queryParams.promoPrice"
          placeholder="请输入促销价格/晚"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['manage:roomtype:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['manage:roomtype:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:roomtype:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['manage:roomtype:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 表单 -->
    <el-table v-loading="loading" :data="roomtypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" min-width="60" align="center" prop="roomTypeId" />
      <el-table-column label="房型名称" align="center" min-width="220" prop="roomName" />
      <!-- <el-table-column label="英文名称" align="center" min-width="220" prop="englishName" /> -->

      <!-- <el-table-column label="房型分类" align="center" min-width="150" prop="roomClass">
        <template #default="scope">
          <dict-tag :options="room_class" :value="scope.row.roomClass"/>
        </template>
</el-table-column> -->
      <el-table-column label="基础价格/晚" align="center" prop="basePrice" />
      <el-table-column label="基础配置信息" align="center" prop="config" />

      <el-table-column label="促销价格/晚" align="center" prop="promoPrice" />
      <el-table-column label="效果图" align="center" prop="coverImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.coverImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="早餐类型" align="center" prop="breakfastType">
        <template #default="scope">
          <dict-tag :options="breakfast_type" :value="scope.row.breakfastType" />
        </template>
      </el-table-column>

      <!-- <el-table-column label="房型面积" align="center" prop="floorArea" /> -->
      <!-- <el-table-column label="最大入住人数" align="center" min-width="120" prop="maxOccupancy" /> -->
      <!-- <el-table-column label="起始楼层" align="center" prop="startFloor" /> -->
      <!-- <el-table-column label="结束楼层" align="center" prop="endFloor" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="wdm_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="商家ID" align="center" prop="merchantId" /> -->
      <el-table-column label="排序序号" align="center" prop="sortOrder" />
      <el-table-column label="标记信息" align="center" prop="tags">
        <template #default="scope">
          <dict-tag :options="tags" :value="scope.row.tags" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="220" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:roomtype:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:roomtype:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改房型信息对话框 -->
    <el-dialog :title="title" v-model="open" width="40%" append-to-body>
      <el-form ref="roomtypeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家ID" prop="merchantId">
          <el-input v-model="form.merchantId" placeholder="请输入商家ID" />
        </el-form-item>
        <el-form-item label="房型名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入房型名称" />
        </el-form-item>
        <el-form-item label="英文名称" prop="englishName">
          <el-input v-model="form.englishName" placeholder="请输入英文名称" />
        </el-form-item>
        <el-form-item label="房型分类" prop="roomClass">
          <el-select v-model="form.roomClass" placeholder="请选择房型分类">
            <el-option v-for="dict in room_class" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="早餐类型" prop="breakfastType">
          <el-select v-model="form.breakfastType" placeholder="请选择早餐类型">
            <el-option v-for="dict in breakfast_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序序号" prop="sortOrder">
          <el-input v-model="form.sortOrder" placeholder="请输入排序序号" />
        </el-form-item>
        <el-form-item label="标记信息" prop="tags">
          <el-select v-model="form.tags" placeholder="请选择标记信息">
            <el-option v-for="dict in tags" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配置信息" prop="config">
          <el-input v-model="form.config" rows="5" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="基础价格" prop="basePrice">
          <el-input v-model="form.basePrice" placeholder="请输入基础价格/晚" />
        </el-form-item>
        <el-form-item label="促销价格" prop="promoPrice">
          <el-input v-model="form.promoPrice" placeholder="请输入促销价格/晚" />
        </el-form-item>
        <el-form-item label="效果图" prop="coverImage">
          <image-upload v-model="form.coverImage" />
        </el-form-item>

        <!-- <el-form-item label="房型面积" prop="floorArea">
          <el-input v-model="form.floorArea" placeholder="请输入房型面积" />
        </el-form-item>
        <el-form-item label="最大入住人数" label-width="100px" prop="maxOccupancy">
          <el-input v-model="form.maxOccupancy" placeholder="请输入最大入住人数" />
        </el-form-item>
        <el-form-item label="起始楼层" prop="startFloor">
          <el-input v-model="form.startFloor" placeholder="请输入起始楼层" />
        </el-form-item>
        <el-form-item label="结束楼层" prop="endFloor">
          <el-input v-model="form.endFloor" placeholder="请输入结束楼层" />
        </el-form-item> -->

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in wdm_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio v-for="dict in del_flag" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>


      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Roomtype">
import { listRoomtype, getRoomtype, delRoomtype, addRoomtype, updateRoomtype } from "@/api/ruoyi/roomtype"

const { proxy } = getCurrentInstance()
const { wdm_status, breakfast_type, room_class, tags, del_flag } = proxy.useDict('wdm_status', 'breakfast_type', 'room_class', 'tags', 'del_flag')

const roomtypeList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomName: null,
    englishName: null,
    roomClass: null,
    breakfastType: null,
    floorArea: null,
    maxOccupancy: null,
    startFloor: null,
    endFloor: null,
    status: null,
    version: null,
    merchantId: null,
    sortOrder: null,
    tags: null,
    config: null,
    basePrice: null,
    promoPrice: null,
    coverImage: null
  },
  rules: {
    roomName: [
      { required: true, message: "房型名称不能为空", trigger: "blur" }
    ],
    merchantId: [
      { required: true, message: "商家ID不能为空", trigger: "blur" }
    ],
    config: [
      { required: true, message: "基础配置信息不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询房型信息列表 */
function getList() {
  loading.value = true
  listRoomtype(queryParams.value).then(response => {
    // roomtypeList.value = response.rows
    // 过滤已删除数据（假设 delFlag 字段标识删除状态）
    roomtypeList.value = response.rows.filter(
      (item) => item.delFlag !== "2"
    );
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    roomTypeId: null,
    roomName: null,
    englishName: null,
    roomClass: null,
    breakfastType: null,
    floorArea: null,
    maxOccupancy: null,
    startFloor: null,
    endFloor: null,
    status: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    merchantId: null,
    sortOrder: null,
    tags: null,
    config: null,
    basePrice: null,
    promoPrice: null,
    coverImage: null
  }
  proxy.resetForm("roomtypeRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.roomTypeId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加房型信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _roomTypeId = row.roomTypeId || ids.value
  getRoomtype(_roomTypeId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改房型信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roomtypeRef"].validate(valid => {
    if (valid) {
      if (form.value.roomTypeId != null) {
        updateRoomtype(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRoomtype(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _roomTypeIds = row.roomTypeId || ids.value
  proxy.$modal.confirm('是否确认删除房型信息编号为"' + _roomTypeIds + '"的数据项？').then(function () {
    return delRoomtype(_roomTypeIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/roomtype/export', {
    ...queryParams.value
  }, `roomtype_${new Date().getTime()}.xlsx`)
}

getList()
</script>
